import Vue from 'vue'
import VueRouter from 'vue-router'

// import Home from '@/views/Home'
// import Login from '@/views/Login'
// import Register from '@/views/Register'
// import ConcertHall from '@/views/ConcertHall'
// import SongList from '@/views/SongList'
// import Artist from '@/views/Artist'
// import MusicVideo from '@/views/MusicVideo'
// import Personal from '@/views/Personal'

const Home = () => import('@/views/Home')
const Login = () => import('@/views/Login')
const Register = () => import('@/views/Register')
const ConcertHall = () => import('@/views/ConcertHall')
const SongList = () => import('@/views/SongList')
const Artist = () => import('@/views/Artist')
const MusicVideo = () => import('@/views/MusicVideo')
const Personal = () => import('@/views/Personal')


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path:'/login',
    component: Login,
    meta:{title:'用户登录'}
  },
  {
    path:'/register',
    component: Register,
    meta:{title:'用户注册'}
  },
  {
    path: '/home',
    redirect: '/concerthall'
  },
  {
    path: '/home',
    component: Home,
    meta:{title:'首页'},
    children:[
      {
        path:'/concerthall',
        component:ConcertHall
      },
      {
        path:'/songlist',
        component:SongList
      },
      {
        path:'/artist',
        component:Artist
      },
      {
        path:'/musicvideo',
        component:MusicVideo
      },
      {
        path:'/personal',
        component:Personal
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') return next()
  const cookieStr = window.sessionStorage.getItem('cookie')
  if (!cookieStr) return next('/login')
  next()
})

// 后置路由守卫
router.afterEach(to => {
  document.title = to.meta.title || '组半天队项目ing'
})

export default router
